<?php


namespace Module\Example;


use Framework\Console;
use Framework\DataProvider;
use ZM\Annotation\CQ\CQCommand;
use ZM\Annotation\Http\Middleware;
use ZM\Annotation\Http\RequestMapping;
use ZM\Annotation\Swoole\SwooleEventAt;
use ZM\Connection\CQConnection;
use ZM\Utils\ZMRequest;
use ZM\Utils\ZMUtil;

/**
 * Class Hello
 * @package Module\Example
 * @since 1.0
 */
class Hello
{
    /**
     * 在机器人连接后向终端输出信息
     * @SwooleEventAt("open",rule="connectType:qq")
     * @param $conn
     */
    public function onConnect(CQConnection $conn)
    {
        Console::info("机器人 " . $conn->getQQ() . " 已连接！");
    }

    /**
     * 向机器人发送"你好"，即可回复这句话
     * @CQCommand("你好")
     */
    /*public function hello()
    {

        //return DataProvider::getFrameworkLink();
        //return "你好啊，我是由炸毛框架构建的机器人！";
    }*/

    /**
     * @CQCommand(".reload")
     */
    public function reload()
    {
        context()->reply("reloading...");
        ZMUtil::reload();
    }

    /**
     * @CQCommand("随机数")
     * @CQCommand(regexMatch="*从*到*的随机数")
     * @param $arg
     */
    public function randNum($arg)
    {
        // 获取第一个数字类型的参数
        $num1 = context()->getArgs($arg, ZM_MATCH_NUMBER, "请输入第一个数字");
        // 获取第二个数字类型的参数
        $num2 = context()->getArgs($arg, ZM_MATCH_NUMBER, "请输入第二个数字");
        $a = min(intval($num1), intval($num2));
        $b = max(intval($num1), intval($num2));
        // 回复用户结果
        context()->reply("随机数是：" . mt_rand($a, $b));
    }

    /**
     * @CQCommand("花价")
     * @param $args
     * @return string
     */
    public function flower($args)
    {
        // TODO 此处是否可以再使用 CQCommand(regexMatch="", alias={""})
        $defaultServer = '天鹅坪';
        $flowerArray = ['绣球花', '郁金香', '牵牛花', '荧光菌', '百合花', '玫瑰花', '绣球', '牵牛', '荧光', '百合', '玫瑰'];
        $name = context()->getArgs($args, ZM_MATCH_ALL, "小可爱你要查哪种花？");
        if (strstr($name, '绣球')) $name = $flowerArray[0];
        if (strstr($name, '牵牛')) $name = $flowerArray[2];
        if (strstr($name, '荧光')) $name = $flowerArray[3];
        if (strstr($name, '百合')) $name = $flowerArray[9];
        if (strstr($name, '玫瑰')) $name = $flowerArray[10];

        if (!in_array($name, $flowerArray)) return '查询不到相关信息!';

        /*$result = ZMRequest::request('https://next.jx3box.com/api/flower/price/rank', [
            'method' => 'GET',
            'data' => [
                'server' => $defaultServer,
                'flower' => $name
            ]
        ]);*/

        $result = ZMRequest::get("https://next.jx3box.com/api/flower/price/rank?server={$defaultServer}&flower={$name}");

        //$result = file_get_contents("https://next.jx3box.com/api/flower/price/rank?server={$defaultServer}&flower={$name}");

        //var_dump($result);

        //$result = get_json("https://next.jx3box.com/api/flower/price/rank?server={$defaultServer}&flower={$name}");

        $res = '天鹅坪-' . $name;
        foreach (json_decode($result) as $key => $value) {
            //$res .= "\n" . $key . "(最高价：" . $value['max'] . ")\n"."收购：" . $value['maxLine'][0] . " " . $value['maxLine'][1] . " " . $value['maxLine'][2];
            //$res .= "\n" . $key . "\n" . "正在收购：" . trim($value['maxLine'][0]) . "|" . trim($value['maxLine'][1]) . "|" . trim($value['maxLine'][2]);
            $res .= "\n" . $key . "\n" . "正在收购：" . trim($value->maxLine[0]) . "|" . trim($value->maxLine[1]);
        }
        context()->reply($res);
    }

    /**
     * @CQCommand("骚话")
     */
    public function saohua()
    {
        $keyword = file($GLOBALS['config']['zm_data'] . 'saohua.txt');
        $randWord = trim($keyword[mt_rand(0, count($keyword) - 1)]);

        context()->reply(iconv("GB2312", "UTF-8", $randWord));
    }

    /**
     * 中间件测试的一个示例函数
     * @RequestMapping("/httpTimer")
     * @Middleware("timer")
     */
    public function timer()
    {
        eval(ZM_BREAKPOINT);
        return "This page is used as testing TimerMiddleware! Do not use it in production.";
    }

    /**
     * 默认示例页面
     * @RequestMapping("/index")
     * @RequestMapping("/")
     */
    public function index()
    {
        return "Hello Zhamao!";
    }


    /**
     * 框架会默认关闭未知的WebSocket链接，因为这个绑定的事件，你可以根据你自己的需求进行修改
     * @SwooleEventAt(type="open",rule="connectType:unknown")
     */
    public function closeUnknownConn()
    {
        Console::info("Unknown connection , I will close it.");
        context()->getConnection()->close();
    }
}
